﻿using Engine.Graphics;
using Game;

namespace Mekiasm
{
    public class Submersible : ClothingItem,Interface.IChargeAbleItem
    {
        public static int Index;

        public float FallResilience = 0f;

        public ComponentEnergyMachine.FuelType ChargeFuelType => ComponentEnergyMachine.FuelType.Gas;

        public long MaxAmount => GetMaxPower();

        public Submersible() : base( "潜水器")
        {
            Clothing.ArmorProtection = 0.6f;
        }

        public override void ClothingEquipedUpdate(int value, ComponentPlayer componentPlayer)
        {
            SaveItemSystem.GetBlockData(value, out Common.SaveItem toolSaveItem);
            if (toolSaveItem != null && toolSaveItem.fuelAmount >= 10)
            {
                if (componentPlayer.ComponentHealth.Air < 0.9f)
                {
                    SaveItemSystem.ChangePower(componentPlayer.ComponentMiner.Inventory, componentPlayer.ComponentMiner.Inventory.ActiveSlotIndex, -(int)((1f - componentPlayer.ComponentHealth.Air) * 100));
                    componentPlayer.ComponentHealth.Air = 1f;
                }
            }
            if (toolSaveItem == null || toolSaveItem.Power == 0)
            {
                Clothing.ArmorProtection = 0f;
            }
            else
            {
                Clothing.ArmorProtection = 0.6f;
            }
        }

        public override int GetMaxPower()
        {
            return 10240;
        }

        public override bool IsCrushWhenNoPower()
        {
            return false;
        }

        public override bool IsClothing()
        {
            return true;
        }

        public override ClothingSlot GetClothingSlot()
        {
            return ClothingSlot.Torso;
        }

        public override Texture2D GetTexture()
        {
            return ContentManager.Get<Texture2D>("Mekiasm/Textures/Clothing/Submersible");
        }

        public override bool IsOuter()
        {
            return true;
        }

        public override float GetBlockHealth(int value)
        {
            SaveItemSystem.GetBlockData(value, out var d);
            if (d == null) return -1f;
            else return GetCurrentAmount(value) / (float)GetMaxPower();
        }


        public long GetCurrentAmount(int value)
        {
            SaveItemSystem.GetBlockData(value, out var d);
            if (d == null) return 0;
            else return d.fuelAmount;
        }

        public int SetCurrentAmount(int value, int fuelValue, long varAmount)
        {
            return SaveItemSystem.ChangePower(value, varAmount, fuelValue);
        }

        public int GetChargeFuelValue(int value)
        {
            SaveItemSystem.GetBlockData(value, out var d);
            if (d == null) return 0;
            else return d.fuelValue;
        }

        public bool CanAcceptFuel(int value)
        {
            if (value == Oxygen.Index) return true;
            return false;
        }
    }
}
